import axios from 'axios'
import qs from "qs";
import { ElMessage } from "element-plus";
import { useRoute, useRouter } from "vue-router";
const router = useRouter();
const to = (url) => {
  router.push(url)
}

const reuqest = axios.create({
  baseURL: 'http://43.140.221.237:10001'
})

reuqest.interceptors.request.use(
  config => {
    // 设置token start
    let accessToken = localStorage.getItem('accessToken');
    if (accessToken && accessToken !== '') {
      config.headers['Token'] = accessToken;
    }
    // 设置token end
    return config;
  },
  error => Promise.reject(error)
);

reuqest.interceptors.response.use(
  response => {
    // 回设置token start
    let accessToken = response.headers['authorization'];
    if (accessToken && accessToken !== '') {
      localStorage.setItem('accessToken', accessToken);
    }
    // 回设置token end
    return response.data;
  },

  // 请求异常信息提示处理
  error => {
    console.log(error);
    if (error.response.status === 412) {
      ElMessage({
        message: "登录失效，请重新登录",
        type: "error",
      })
      localStorage.removeItem('userInfo');
      localStorage.removeItem("accessToken")
      to('/')
    }
    if (error.response.status === 500) {
      ElMessage({
        message: "500错误，请联系开发者",
        type: "error",
      })
    }
    return Promise.reject(error);
  })

const login = (userName, passWord) => {
  return reuqest.post('/login/default', {
    userName, passWord
  })
}

const loginEmail = (email, passWord) => {
  return reuqest.post('/login/email', {
    email, passWord
  })
}

const register = (email, userName, passWord, captcha) => {
  return reuqest.post('/register', {
    email, userName, passWord, captcha
  })
}

const sendCaptcha = (email) => {
  return reuqest.get('/sendCaptcha', {
    params: { email }
  })
}

const forgetPassword = (email, captcha, newPassword) => {
  return reuqest.post('/findMyPassword', {
    email, captcha, newPassword
  })
}

const handleTrainLog = (id) => {
  return reuqest.post('/view/handleTrainLog', {
    id
  })
}

const HandleLayerLog = (id) => {
  return reuqest.post('/view/handleLayerLog', {
    id
  })
}
const changeNickName = (uuid, newNickName) => {
  return reuqest.post('/userCenter/changeNickName', {
    uuid, newNickName
  })
}

const getCommunity = (pagination, amount, rule) => {
  return reuqest.get('/community', {
    params: {
      pagination, amount, rule
    }
  })
}
const communityDetail = (articleId, uuid) => {
  return reuqest.get('/community/detail', {
    params: {
      articleId, uuid
    }
  })
}

const communitySendCommit = (articleId, content, uuid) => {
  return reuqest.post('/community/sendCommit', {
    articleId, content, uuid
  })
}
const communityArticleLike = (articleId, like, uuid) => {
  return reuqest.post('/community/articleLike', {
    articleId, like, uuid
  })
}
const communityReply = (commitId, uuid, content) => {
  return reuqest.post('/community/reply', {
    commitId, uuid, content
  })
}
const handlePreLog = (id) => {
  return reuqest.post('/view/handlePreLog', {
    id
  })
}

const commitLike = (commitId, like, uuid) => {
  return reuqest.post('/community/commitLike', {
    commitId, like, uuid
  })
}

const replyLike = (replyId, like, uuid) => {
  return reuqest.post('/community/replyLike', {
    replyId, like, uuid
  })
}
const getAllLog = (uuid) => {
  return reuqest.post('/view/getList', {
    uuid
  })
}
const commitReply = () => {
  return reuqest.post('/community/commitReply', {

  })
}
const getAllTrainLog = (uuid) => {
  return reuqest.post('/view/getAllTrainLog', {
    uuid
  })
}
const getAllLayerLog = (uuid) => {
  return reuqest.post('/view/getAllLayerLog', {
    uuid
  })
}

const SubmitArticle = (content, uuid, title, coverImage) => {
  return reuqest.post('/userCenter/submitArticle', {
    content, uuid, title, coverImage
  })
}
const deleteTrainLog = (id)=>{
  return reuqest.post('/view/deleteTrainLog', {
    id
  })
}
const deleteLayerLog = (id)=>{
  return reuqest.post('/view/deleteLayerLog', {
    id
  })
}
const deletePreLog = (id)=>{
  return reuqest.post('/view/deletePreLog', {
    id
  })
}


export {
  register, sendCaptcha, login, loginEmail, forgetPassword, handleTrainLog, HandleLayerLog, changeNickName, getCommunity,
  communityDetail, communitySendCommit, communityArticleLike, communityReply, handlePreLog, commitLike, replyLike, getAllLog,
  commitReply, getAllTrainLog, getAllLayerLog, SubmitArticle,deleteTrainLog,deleteLayerLog,deletePreLog
}